package  fs.component{
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import fs.component.event.ComponentEvent;
	import fs.component.popup.AbstractDialog;
	import fs.component.popup.PopupManagement;
	
	/**
	 * ...
	 * @author F.S.
	 */
	public class MessageBox extends AbstractDialog {
		public static const OK:int = 1;
		public static const CANCEL:int = 1;
		
		public static const INPUT:int = 2;
		
		private var _tf_content:TextField;
		private var _btn_ok:TextButton;
		private var _type:int;
		
		public var onOk:Function;
		
		public function MessageBox(use_factory_methold:Object=null) {
			
			
			
			_tf_content = new TextField();
			_tf_content.height = 20;
			//_tf_content.selectable = false;
			_tf_content.wordWrap = true;
			_tf_content.autoSize = TextFieldAutoSize.LEFT;
			addContent(_tf_content);
			
			_btn_ok = new TextButton();
			_btn_ok.title = 'OK';
			_btn_ok.onClick = on_ok_click;
			addContent(_btn_ok);
		}
		
		private function on_ok_click(e:ComponentEvent):void {
			if (_type | INPUT && onOk != null) {
				onOk.apply(null,[_tf_content.text]);
			}else if (onOk != null) {
				onOk.apply(null, [""]);
			}
			close();
		}
		
		public static function show(parent:DisplayObjectContainer, content:String = "", title:String = "",type:int = OK):MessageBox {
			var instance:MessageBox = PopupManagement.popup(MessageBox, parent) as MessageBox;
			instance.show_dialog(parent, content, title, type);
			return instance;
		}
		private function show_dialog(parent:DisplayObjectContainer, content:String , title:String, type:int ):int {
			_type = type;			
			
			super.title = title;
			
			_tf_content.text = content;
			_tf_content.width = 200;
			
			_tf_content.border = (type & INPUT) != 0;
			_tf_content.type = _tf_content.border?TextFieldType.INPUT:TextFieldType.DYNAMIC;
			_tf_content.multiline = _tf_content.border?false:true;
			_tf_content.height = 20;
			_tf_content.autoSize = _tf_content.border?TextFieldAutoSize.NONE:TextFieldAutoSize.LEFT;
			
			_btn_ok.onClick = on_ok_click;
			resize();
			return 1;
		}
		
		override protected function rearrangeUi():void {
			_btn_ok.y = _tf_content.y + _tf_content.height + 10;
			
			_btn_ok.x = 100 - _btn_ok.width / 2;
		}
		
		private function close():void {
			PopupManagement.close(this);
		}
	}

}